<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Viptwolist extends Model
{
    protected $table = 'vipuser_class';
    public $timestamps = false;
    protected $guarded = [
        'id', 'name', 'avatar', 'weixin', 'ispartner', 'off_line_time', 'created_at', 'updated_at'
    ];

    public function getData($search = [], $page = 1, $length = 20){
        $viptwolistList = array();
        $viptwolistList['pageData'] = array();
        $date = date('Y-m-d H:i:s', time());

        $sql = \DB::table($this->table.' as p')
            ->select('p.id','p.user_level','u.nick_name as name','u.headimgurl','u.totalPoints','u.presentPoints','u.inviteCount as invitCount','t.nick_name as inviter')
            ->leftjoin('users as u', 'u.id' , '=' , 'p.user_id')
            ->leftjoin('users as t', 'p.inviter_id' , '=' , 't.id')
            ->where('u.invitecount',0)
            ->whereNull('u.deleted_at')
            ->whereNull('p.deleted_at');

        if(isset($search['weixin'])){
            $sql->where('weixin', 'like', '%'.$search['weixin'].'%');
        }
        if(isset($search['name'])){
            $sql->where('name', 'like', '%'.$search['name'].'%');
        }

        $viptwolistList['pageData']['count']     = $sql->count();
        $viptwolistList['pageData']['lastPage']  = ceil($viptwolistList['pageData']['count'] / $length);

        $viptwolistList['list'] = $sql->orderBy('u.created_at', 'desc')
            ->skip(($page-1) * $length)->take($length)
            ->get();

        return $viptwolistList;
    }


    public function saveData($data){
        return \DB::table($this->table)->insert($data);
    }

    public function setData($id,$data){
        return \DB::table($this->table)->where('id',$id)->update($data);
    }

    public function delData($id,$data){
        return \DB::table($this->table)->where('id',$id)->update($data);
    }


}